// ----------------------------------------------------------------------------//
// ContentVlaueQuery.java
// Created by user On 2012-11-2
package com.demo.framework.db.query;

import java.util.Arrays;

import android.content.ContentValues;
import android.database.Cursor;

import com.demo.framework.db.DB;
import com.demo.framework.db.QueryResultHandler;

/**
 *
 */
public class ContentValuesQuery implements QueryResultHandler<ContentValues> {
	private int[] types;
	private int defaultType = DB.S;

	public static ContentValuesQuery forTypes(final int... types) {
		ContentValuesQuery cvq = new ContentValuesQuery();
		cvq.types = types;
		return cvq;
	}

	public static ContentValuesQuery forInts(int nInt) {
		ContentValuesQuery cvq = new ContentValuesQuery();
		cvq.types = new int[nInt];
		Arrays.fill(cvq.types, DB.I);
		return cvq;
	}
	
	public static ContentValuesQuery forStrings() {
		ContentValuesQuery cvq = new ContentValuesQuery();
		return cvq;
	}

	private ContentValuesQuery() {
	}

	@Override
	public ContentValues handle(Cursor cursor, int numOfCols) {
		ContentValues cv = new ContentValues();
		for (int i = 0; i < numOfCols; ++i) {
			String name = cursor.getColumnName(i);
			if (cursor.isNull(i)) {
				cv.putNull(name);
			} else {
				int type = getType(i);
				switch (type) {
				case DB.I:
					cv.put(name, cursor.getInt(i));
					break;
				case DB.F:
					cv.put(name, cursor.getFloat(i));
					break;
				case DB.S:
					cv.put(name, cursor.getString(i));
					break;
				case DB.B:
					cv.put(name, cursor.getBlob(i));
					break;
				}
			}
		}
		return cv;
	}

	protected int getType(int i) {
		return (types == null || i >= types.length) ? defaultType : types[i];
	}
}

// ----------------------------------END---------------------------------------//